package fr.upstlse.glre.client.widgets.modelData;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.vo.user.UserRoleVOGWT;
import fr.upstlse.glre.client.vo.user.UserVOGWT;

public class UserModelData extends BaseModelData {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2356437422445614305L;
	
	public UserModelData(UserVOGWT pUserVOGWT) {
		set(RfgConstante.USER_MODELDATA_ID, pUserVOGWT.getId());
		set(RfgConstante.USER_MODELDATA_LOGIN, pUserVOGWT.getLogin());
		set(RfgConstante.USER_MODELDATA_MOTDEPASSE, pUserVOGWT.getPassword());
		set(RfgConstante.USER_MODELDATA_ROLE, pUserVOGWT.getRole());
	}
	
	public UserModelData(ModelData pModelData) {
		set(RfgConstante.USER_MODELDATA_ID, pModelData.get(RfgConstante.USER_MODELDATA_ID));
		set(RfgConstante.USER_MODELDATA_LOGIN, pModelData.get(RfgConstante.USER_MODELDATA_LOGIN));
		set(RfgConstante.USER_MODELDATA_MOTDEPASSE, pModelData.get(RfgConstante.USER_MODELDATA_MOTDEPASSE));
		set(RfgConstante.USER_MODELDATA_ROLE, pModelData.get(RfgConstante.USER_MODELDATA_ROLE));
	}
	
	public static List<UserModelData> toUserModelDatas(List<UserVOGWT> pUserVOGWTs) {
		ArrayList<UserModelData> lUserModelDatas = new ArrayList<UserModelData>(pUserVOGWTs.size());
		for(UserVOGWT lUserVOGWT : pUserVOGWTs) {
			UserModelData lUserModelData = new UserModelData(lUserVOGWT);
			lUserModelDatas.add(lUserModelData);
		}
		
		return lUserModelDatas;
	}
	
	public UserVOGWT toUserVOGWT() {
		UserVOGWT lUserVOGWT = new UserVOGWT();
		lUserVOGWT.setId(((Long) get(RfgConstante.USER_MODELDATA_ID)).longValue());
		lUserVOGWT.setLogin((String) get(RfgConstante.USER_MODELDATA_LOGIN));
		lUserVOGWT.setPassword((String) get(RfgConstante.USER_MODELDATA_MOTDEPASSE));
		lUserVOGWT.setRole((String) get(RfgConstante.USER_MODELDATA_ROLE));
		return lUserVOGWT;
	}
	
	public static List<BaseModelData> getRoles() {
		Collection<UserRoleVOGWT> lRoles = UserRoleVOGWT.getUserRoleVOGWTs().values();
		ArrayList<BaseModelData> lRoleModelDatas = new ArrayList<BaseModelData>(lRoles.size());
		
		for(UserRoleVOGWT lRole : lRoles) {
			BaseModelData lRoleModelData = new BaseModelData();
			lRoleModelData.set(RfgConstante.ROLE_MODELDATA_ROLE, lRole.getValue());
			lRoleModelDatas.add(lRoleModelData);
		}
		
		return lRoleModelDatas;
	}

}
